package com.ruisoft.business.employee.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ruisoft.base.controller.BaseController;
import com.ruisoft.business.employee.service.EmployeeService;
import com.ruisoft.core.json.JSONMap;

import org.apache.log4j.Logger;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

/**
 * 
 * @author LFC
 *
 */

@Controller
@RequestMapping("/employee/")
public class EmployeeController extends BaseController {

	private static final Logger LOG = Logger.getLogger(EmployeeController.class);

	@Autowired
	private EmployeeService employeeService;

	/**
	 * 查询员工基本信息
	 */
	@RequestMapping("query")
	public void query() {
		JSONMap<String, Object> json = new JSONMap<String, Object>(JSONMap.TYPE.OBJECT);
		if (queryData == null)
			queryData = getQueryData();
		try {
			List<JSONObject> result = employeeService.searchForPage(queryData);
			queryResponse(queryData, result);
		} catch (Exception e) {
			LOG.error("查询员工信息发生错误", e);
			json.put("status", "-2");
			json.put("msg", "执行查询员工信息操作发生错误");
		} finally {
			try {
				response(json);
			} catch (IOException e) {
				LOG.error("查询返回信息时发生错误", e);
			}
			queryData = null;
		}
	}
	
	/**
	 * 查询单个员工的基本信息
	 * @param userid 员工编号
	 */
	@RequestMapping("searchUser/{userid}")
	public void searchUser(@PathVariable String userid,HttpServletRequest request,HttpServletResponse response)throws Exception {
		JSONMap<String, Object> json = new JSONMap<String, Object>(JSONMap.TYPE.OBJECT);
		try{
			JSONObject result = employeeService.searchUser(userid,request);
			json.put("data", result);
			json.put("status", 1);
		} catch (Exception e) {
			LOG.error("查询员工信息发生错误", e);
			json.put("status", "-2");
			json.put("msg", "执行查询员工信息操作发生错误");
		} finally {
			try {
				response(json);
			} catch (IOException e) {
				LOG.error("查询返回信息时发生错误", e);
			}
		}
	}
	
	/**
	 * 保存员工的基本信息
	 */
	@RequestMapping("saveEmployee")
	public void saveEmployee() {
		JSONMap<String, Object> json = new JSONMap<String, Object>(JSONMap.TYPE.OBJECT);
		try {
			JSONObject reqData = getReqData();
			JSONObject result = new JSONObject();
			result = employeeService.saveEmployee(reqData);
			json.put("result", result);
			json.put("status", "1");
		} catch (Exception e) {
			LOG.error("保存员工信息信息发生错误", e);
			json.put("status", "-2");
			json.put("msg", "保存员工信息信息发生错误");
		} finally {
			try {
				response(json);
			} catch (IOException e) {
				LOG.error("返回信息时发生错误", e);
			}
		}
	}
	
	/**
	 * 保存员工的人脸模板
	 */
	@RequestMapping("saveFace")
	public void saveFace() {
		JSONMap<String, Object> json = new JSONMap<String, Object>(JSONMap.TYPE.OBJECT);
		try {
			JSONObject reqData = getReqData();
			JSONObject result = new JSONObject();
			result = employeeService.saveFace(reqData);
			json.put("result", result);
			json.put("status", "1");
		} catch (Exception e) {
			LOG.error("保存员工信息信息发生错误", e);
			json.put("status", "-2");
			json.put("msg", "保存员工信息信息发生错误");
		} finally {
			try {
				response(json);
			} catch (IOException e) {
				LOG.error("返回信息时发生错误", e);
			}
		}
	}
	
	/**
	 * 保存员工信息与考勤设备的对照关系
	 */
	@RequestMapping("saveMachUser")
	public void saveMachUser() {
		JSONMap<String, Object> json = new JSONMap<String, Object>(JSONMap.TYPE.OBJECT);
		try {
			JSONObject reqData = getReqData();
			JSONObject result = new JSONObject();
			result = employeeService.saveMachUser(reqData);
			json.put("result", result);
			json.put("status", "1");
		} catch (Exception e) {
			LOG.error("保存员工信息信息发生错误", e);
			json.put("status", "-2");
			json.put("msg", "保存员工信息信息发生错误");
		} finally {
			try {
				response(json);
			} catch (IOException e) {
				LOG.error("返回信息时发生错误", e);
			}
		}
	}
	
	
	 @RequestMapping("updateEmployee")
    public void updateEmployee() {
        JSONMap<String, Object> json = new JSONMap<String, Object>(JSONMap.TYPE.OBJECT);
        try {
            JSONObject reqData = getReqData();
            JSONObject result = new JSONObject();
            result = employeeService.updateEmployee(reqData);
            json.put("data", result);
            json.put("status", "1");
        } catch (Exception e) {
            LOG.error("更新员工基本信息发生错误", e);
            json.put("status", "-2");
            json.put("msg", "更新员工基本信息发生错误");
        } finally {
            try {
                response(json);
            } catch (IOException e) {
                LOG.error("返回信息时发生错误", e);
            }
        }
    }
	 
	 @RequestMapping("updateEmployeeValid")
	    public void updateEmployeeValid() {
	        JSONMap<String, Object> json = new JSONMap<String, Object>(JSONMap.TYPE.OBJECT);
	        try {
	            JSONObject reqData = getReqData();
	            JSONObject result = new JSONObject();
	            result = employeeService.updateEmployeeValid(reqData);
	            json.put("data", result);
	            json.put("status", "1");
	        } catch (Exception e) {
	            LOG.error("更新员工基本信息发生错误", e);
	            json.put("status", "-2");
	            json.put("msg", "更新员工基本信息发生错误");
	        } finally {
	            try {
	                response(json);
	            } catch (IOException e) {
	                LOG.error("返回信息时发生错误", e);
	            }
	        }
	    }
	 
	  @RequestMapping("deleteEmployee/{id}")
	    public void deleteEmployee(@PathVariable String id) throws Exception{
	        int status = employeeService.deleteEmployee(id);
	        JSONMap<String, Object> json = new JSONMap<String, Object>(JSONMap.TYPE.OBJECT);
	        json.put("status", status);
	        response(json);
	    }
	
}
